<template>
    <div>
        <div style="width: 473px">
            <el-input
                    placeholder="输入部门名称进行搜索..."
                    prefix-icon="el-icon-search"
                    v-model="filterText">
            </el-input>
            <el-tree
                    :data="deps"
                    :props="defaultProps"
                    :filter-node-method="filterNode"
                    :expand-on-click-node="false"
                    ref="tree">
                <span class="custom-tree-node" slot-scope="{ node, data }"
                      style="display: flex;justify-content: space-between;width: 100%">
                    <span>{{ data.name }}</span>
                    <span>
                        <el-button
                                class="depBtn"
                                type="primary"
                                size="mini"
                                icon="el-icon-plus"
                                @click="showAddDepDialog(data)">
                        </el-button>
                        <el-button
                                class="depBtn"
                                type="danger"
                                size="mini"
                                icon="el-icon-delete"
                                @click="() => deleteDep(data)">
                        </el-button>
                    </span>
                </span>
            </el-tree>
            <el-dialog
                    title="添加部门"
                    :visible.sync="dialogVisible"
                    width="30%">
                <div>
                    <table>
                        <tr>
                            <td>
                                <el-tag>上级部门</el-tag>
                            </td>
                            <td>
                                {{parentName}}
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <el-tag>部门名称</el-tag>
                            </td>
                            <td>
                                <el-input v-model="dep.name" placeholder="请输入部门名称..."></el-input>
                            </td>
                        </tr>
                    </table>
                </div>
                <span slot="footer" class="dialog-footer">
                    <el-button @click="dialogVisible = false">取 消</el-button>
                    <el-button type="primary" @click="addDep">确 定</el-button>
                </span>
            </el-dialog>
        </div>
        <div></div>
    </div>
</template>

<script>
    export default {
        name: "DepManger",
        data() {
            return {
                filterText: '',
                deps: [],
                defaultProps: {
                    children: 'children',
                    label: 'name'
                },
                dialogVisible: false,
                dep: {
                    name: '',
                    parentId: -1,
                },
                parentName: '',
            }
        },
        mounted() {
            this.initDeps();
        },
        watch: {
            filterText(val) {
                this.$refs.tree.filter(val);
            },
        },
        methods: {
            initDep() {
                this.dep = {
                    name: '',
                    parentId: -1,
                };
                this.parentName = '';
            },
            initDeps() {
                this.getRequest('/system/basic/department/').then(res => {
                    if (res) {
                        this.deps = res;
                    }
                })
            },
            filterNode(value, data) {
                if (!value) return true;
                return data.name.indexOf(value) !== -1;
            },
            showAddDepDialog(data) {
                this.dep.parentId = data.id;
                this.parentName = data.name;
                this.dialogVisible = true;
            },
            addDep() {
                this.postRequest('/system/basic/department/', this.dep).then(res => {
                    if (res) {
                        this.didAddDep(this.deps, res.object);
                        this.dialogVisible = false;
                        this.initDep();
                    }
                })
            },
            didAddDep(deps, dep) {
                for (let i = 0; i < deps.length; i++) {
                    let d = deps[i];
                    if (d.id === dep.parentId) {
                        d.children = d.children.concat(dep);
                        if (d.children.length > 0) {
                            d.isParent = true;
                        }
                        return;
                    } else {
                        this.didAddDep(d.children, dep);
                    }
                }
            },
            deleteDep(data) {
                if (data.isParent) {
                    this.$message.error("此部门为父部门，无法删除");
                } else {
                    this.$confirm('此操作将永久删除该[' + data.name + ']部门, 是否继续?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        this.deleteRequest('/system/basic/department/'+ data.id).then(res => {
                            console.log(data.id);
                            if (res) {
                                this.delDepFromDeps(null, this.deps, data.id);
                            }
                        })
                    }).catch(() => {
                        this.$message({
                            type: 'info',
                            message: '已取消删除'
                        });
                    });
                }
            },
            delDepFromDeps(parent, deps, id) {
                for (let i = 0; i < deps.length; i++) {
                    let dep = deps[i];
                    if (dep.id === id) {
                        deps.splice(i, 1);
                        if (deps.length === 0) {
                            parent.isParent = false;
                        }
                        return;
                    } else {
                        this.delDepFromDeps(dep, dep.children, id);
                    }
                }
            },
        },
    }
</script>

<style scoped>
    .depBtn {
        padding: 3px;
    }
</style>